package ee.wm.ilmataat;

import java.util.List;

import android.location.Location;
import android.util.Log;

public class LocationComparison {
	
	public static Station findClosestStation(Location userLocation, List<Station> stations) {
		Station closestStation = null;
		for(Station station : stations) {
			Location stationLocation = createStationLocation(station);
			float distance = userLocation.distanceTo(stationLocation);
			Log.d("Ilmataat", "Distance from "+station.getName()+" is "+distance);
			station.setDistance(distance);
			if(closestStation == null || closestStation.getDistance() > station.getDistance()) {
				closestStation = station;
			}
		}
		return closestStation;
	}
	
	private static Location createStationLocation(Station station) {
		Location location = new Location("station");
		location.setLongitude(station.getLocation().getLongitude().doubleValue());
		location.setLatitude(station.getLocation().getLatitude().doubleValue());
		return location;
	}

}
